package com.spdg.ring.bo;

import java.util.List;

import cn.wolf.base.BaseBo;
import cn.wolf.http.HttpCallBack;

import com.spdg.ring.RingBaseActivity;
import com.spdg.ring.datamgr.ArticleDataMgr;
import com.spdg.ring.entity.ContentEntity;
import com.spdg.ring.resp.ContentResp;
import com.spdg.ring.sqlite.model.ArticleList;
import com.spdg.ring.task.GetContentTask;

public class ContentBo extends BaseBo implements HttpCallBack<ContentResp> {

	private String url;
	private HttpCallBack<ContentResp> icall;
	private RingBaseActivity mActivity;
	private String fileKey; // 请求的parent key

	public ContentBo(RingBaseActivity activity, String url, String fileKey,
			HttpCallBack<ContentResp> icall) {
		super(activity);
		this.icall = icall;
		this.url = url;
		this.mActivity = activity;
		this.fileKey = fileKey;
	}

	public void request() {
		mActivity.showProgressBar();

		GetContentTask task = new GetContentTask(this, url);
		task.execute();
	}

	@Override
	public void call(ContentResp response) {
		mActivity.hideProgressBar();

		if (response.isSuccess()) {
			ContentEntity entity = response.getmContentEntity();
			if (entity != null) {
				final ArticleDataMgr articleMgr = ArticleDataMgr.getInstance();
				final List<ArticleList> list = entity.getItemList();
				if (list != null && list.size() > 0) {
					// 刷新缓存
					articleMgr.refresh(mActivity, fileKey, list);

					new Thread() {
						public void run() {
							// 更新数据库数据
							articleMgr.saveDataSqlite(mActivity, fileKey, list);
						}
					}.start();

				}
			}

		}

		if (icall != null) {
			icall.call(response);
		}
	}

	@Override
	public void progress(Object... obj) {
		if (icall != null) {
			icall.progress(obj);
		}
	}

	@Override
	public void onNetWorkError() {
		if (icall != null) {
			icall.onNetWorkError();
		}
	}
}
